package cn.krund.lyc.hotlerobot.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


public class BitmapUtil {

    /**
     * 对图片进行二次采样，生成缩略图。放置加载过大图片出现内存溢出
     */
    public static Bitmap createThumbnail(String filePath, int newWidth, int newHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        int originalWidth = options.outWidth;
        int originalHeight = options.outHeight;

        int ratioWidth = originalWidth / newWidth;
        int ratioHeight = originalHeight / newHeight;

        options.inSampleSize = ratioHeight > ratioWidth ? ratioHeight
                : ratioWidth;
        options.inPreferredConfig= Bitmap.Config.RGB_565;
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    public static Bitmap getTransparentBitmap(Bitmap sourceImg, int number) {
        int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
        sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值
        number = number * 255 / 100;
        for (int i = 0; i < argb.length; i++) {
            argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);
        }
        sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Bitmap.Config.ARGB_4444);
        return sourceImg;
    }
}

